home *** CD-ROM | disk | FTP | other *** search
/ Czech Logic, Card & Gambling Games / Logické hry.iso / hry / Fish Fillets / script / share / Pickle.lua < prev    next >
Encoding:
Text File  |  2005-07-16  |  2.4 KB  |  91 lines

  1. ----------------------------------------------
  2. -- Pickle.lua
  3. -- An table serialization utility for lua 5
  4. -- Steve Dekorte, http://www.dekorte.com, April 2000
  5. -- Public Domain
  6. -- Lua 5.0 update by Daan Nusman July 2003
  7. ----------------------------------------------
  8.  
  9. function pickle(t)
  10.   return Pickle:clone():pickle_(t)
  11. end
  12.  
  13. Pickle = {
  14.   clone = function (t) local nt={}; for i, v in pairs(t) do nt[i]=v end return nt end
  15. }
  16.  
  17. function Pickle:pickle_(root)
  18.   if type(root) ~= "table" then 
  19.     error("can only pickle tables, not ".. type(root).."s")
  20.   end
  21.   self._tableToRef = {}
  22.   self._refToTable = {}
  23.   local savecount = 0
  24.   self:ref_(root)
  25.   local s = ""
  26.  
  27.   while table.getn(self._refToTable) > savecount do
  28.     savecount = savecount + 1
  29.     local t = self._refToTable[savecount]
  30.     s = s.."{\n"
  31.     for i, v in pairs(t) do
  32.         if type(v) ~= "function" then
  33.             s = string.format("%s[%s]=%s,\n", s, self:value_(i), self:value_(v))
  34.         end
  35.     end
  36.     s = s.."},\n"
  37.   end
  38.  
  39.   return string.format("{%s}", s)
  40. end
  41.  
  42. function Pickle:value_(v)
  43.   local vtype = type(v)
  44.   if     vtype == "string" then return string.format("%q", v)
  45.   elseif vtype == "number" then return v
  46.   elseif vtype == "boolean" then return tostring(v)
  47.   elseif vtype == "table" then return "{"..self:ref_(v).."}"
  48.   else error("pickle a "..type(v).." is not supported")
  49.   end
  50. end
  51.  
  52. function Pickle:ref_(t)
  53.   local ref = self._tableToRef[t]
  54.   if not ref then 
  55.     if t == self then error("can't pickle the pickle class") end
  56.     table.insert(self._refToTable, t)
  57.     ref = table.getn(self._refToTable)
  58.     self._tableToRef[t] = ref
  59.   end
  60.   return ref
  61. end
  62.  
  63. ----------------------------------------------
  64. -- unpickle
  65. ----------------------------------------------
  66.  
  67. function unpickle_string(s)
  68.   if type(s) ~= "string" then
  69.     error("can't unpickle a "..type(s)..", only strings")
  70.   end
  71.   local tables = loadstring("return "..s)()
  72.   return unpickle_table(tables)
  73. end
  74.   
  75. function unpickle_table(tables)
  76.   if type(tables) ~= "table" then
  77.     error("can't unpickle a "..type(tables)..", only tables")
  78.   end
  79.   for tnum = 1, table.getn(tables) do
  80.     local t = tables[tnum]
  81.     local tcopy = {}; for i, v in t do tcopy[i] = v end
  82.     for i, v in pairs(tcopy) do
  83.       local ni, nv
  84.       if type(i) == "table" then ni = tables[i[1]] else ni = i end
  85.       if type(v) == "table" then nv = tables[v[1]] else nv = v end
  86.       t[ni] = nv
  87.     end
  88.   end
  89.   return tables[1]
  90. end
  91.